package org.javaboy.meeting.controller;

import org.javaboy.meeting.model.Department;
import org.javaboy.meeting.model.Employee;
import org.javaboy.meeting.model.Meeting;
import org.javaboy.meeting.model.Pager;
import org.javaboy.meeting.service.DepartmentService;
import org.javaboy.meeting.service.EmployeeService;
import org.javaboy.meeting.service.MeetingRoomService;
import org.javaboy.meeting.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
public class MeetingController {
    public static final Integer PAGE_SIZE = 10;
    public static  final Integer STATUS=0;

    @Autowired
    MeetingRoomService meetingRoomService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    EmployeeService employeeService;

    @Autowired
    MeetingService meetingService;

    /**
     * 跳转预定会议页面
     * @param model
     * @return
     */
    @RequestMapping({"/bookmeeting","/admin/bookmeeting"} )
    public String bookmeeting(Model model){
        model.addAttribute("mrs",meetingRoomService.getAllMr());
        return "bookmeeting";
    }



    /**
     * 获得所有部门
     * @return
     */
    @RequestMapping("/alldeps")
    //返回json数据
    @ResponseBody
    public List<Department> getAllDeps(){
        return departmentService.getAllDeps();
    }

    /**
     * 根据部门id寻找用户
     * @param depId 部门id
     * @return
     */
    @RequestMapping("/getEmpByDepId")
    @ResponseBody
    public List<Employee> getEmpByDepId(Integer depId){
        return employeeService.getEmpByDepId(depId);
    }

    /**
     * 添加会议信息
     * @param meeting 会议信息的javabean
     * @param mps 需要参加会议员工的id
     * @param session
     * @return
     */
    @RequestMapping("/doAddMeeting")
    public String doAddMeeting(Meeting meeting, Integer[] mps, HttpSession session,Model model) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获得当前登录用户
        Employee currentuser = (Employee) session.getAttribute("currentuser");
        List<Meeting> listMeeting = meetingService.getMeetingByRoomId(meeting.getRoomid());
        for (Meeting meeting1 :listMeeting){
            if (meeting1.getStarttime().getTime()<=meeting.getStarttime().getTime()&&meeting1.getEndtime().getTime()>=meeting.getStarttime().getTime()
            ||meeting1.getStarttime().getTime()<=meeting.getEndtime().getTime()&&meeting1.getEndtime().getTime()>=meeting.getEndtime().getTime()
            ||meeting1.getStarttime().getTime()>=meeting.getStarttime().getTime()&&meeting1.getEndtime().getTime()<=meeting.getEndtime().getTime()){
                model.addAttribute("errormessage","该会议室预定时间期间有会议进行,请另选时段或改变会议室");
                return "forward:/bookmeeting";
            }
        }
        meeting.setReservationistid(currentuser.getEmployeeid());
        meeting.setStatus(0);
        Integer result = meetingService.addMeeting(meeting,mps);
        if (result == 1) {
            return "redirect:/allmeetingsbyPage?start=0";
        }else{
            return "forward:/bookmeeting";
        }
    }

    /**
     * 搜索会议
     * @param meeting 封装会议搜索内容的javaBean
     * @param page 当前页数
     * @param model
     * @return
     */
    @RequestMapping({"/searchmeetings","/admin/searchmeetings"})
    public String getAllMeetings(Meeting meeting, @RequestParam(defaultValue = "1") Integer page, Model model) {
        List<Meeting> meetings= meetingService.getAllMeetings(meeting,page,PAGE_SIZE);
        Long total = meetingService.getTotal(meeting);
        model.addAttribute("meetings",meetings);
        model.addAttribute("total",total);
        model.addAttribute("page", page);
        model.addAttribute("pageNum", total % PAGE_SIZE == 0 ? total / PAGE_SIZE : total / PAGE_SIZE + 1);
        return "searchmeetings";
    }

    /**
     * 会议室分页
     * @param model
     * @param start 当前页面开始项
     * @return
     */
    @RequestMapping({"/allmeetingsbyPage","/admin/allmeetingsbyPage"})
    public String allmeetingsbyPage(Model model,int start){
        Pager<Meeting> meetingPager = meetingService.getMeetingsbyPage(start,10);
        model.addAttribute("mtps",meetingPager);
        model.addAttribute("pagenumber", Math.ceil(meetingPager.getPageStart()/10)+1);
        return "allmeetings";
    }

    /**
     *
     * @param model
     * @param start 当前页面开始项
     * @param meetingname 搜索会议名称
     * @param roomid 搜索会议室id
     * @param reservername 搜索预定姓名
     * @return
     */
    @RequestMapping({"/searchMeetingsbyPage","/admin/searchMeetingsbyPage"})
    public String searchMeetingsbyPage(Model model,int start,String meetingname,String roomid,String reservername){
        Pager<Meeting> meetingPager = meetingService.searchMeetingsbyPage(start,10,meetingname,roomid,reservername);
        model.addAttribute("mtps",meetingPager);
        model.addAttribute("meetingname",meetingname);
        model.addAttribute("roomid",roomid);
        model.addAttribute("reservername", reservername);
        model.addAttribute("pagenumber", Math.ceil(meetingPager.getPageStart()/10)+1);
        return "searchmeetings";
    }


    /**
     * 查看会议详细信息
     * @param model
     * @param meetingid 会议id
     * @return 跳转查看会议详细信息页面
     */
    @RequestMapping({"/meetingdetails","/admin/meetingdetails"})
    public String meetingdetails(Model model,Integer meetingid){
        Meeting meeting = meetingService.getMeetingById(meetingid);
        List<Employee> emps = employeeService.getEmpByMeetingId(meetingid);
        model.addAttribute("meeting",meeting);
        model.addAttribute("emps",emps);
        return "meetingdetails";

    }

    /**
     * 查看用户本身预定的会议
     * @param employeeid 用户的ID
     * @param model
     * @return 返回我的预定的页面
     */
    @RequestMapping({"/mybookings","/admin/mybookings"})
    public String mybookings(Integer employeeid,Model model){

        List<Meeting> mybookings = meetingService.getMyBookingByEmpId(employeeid,STATUS);
//        for (Meeting meeting :mybookings){
//            System.out.println(meeting);
//        }
        model.addAttribute("mybookings",mybookings);
        return "mybookings";
    }

    /**
     * 查看用户需要参与的会议
     * @param employeeid 用户的id
     * @param model
     * @return 返回我的会议的页面
     */
    @RequestMapping({"/mymeetings","/admin/mymeetings"})
    public String mymeetings(Integer employeeid,Model model){
        List<Meeting> mymeetings = meetingService.getMyMeetingByEmpId(employeeid);
        model.addAttribute("mymeetings",mymeetings);
        return "mymeetings";
    }

}
